
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package nac.firefight;

//~--- non-JDK imports --------------------------------------------------------

import nac.utils.RenderUtil;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.StaticBody;
import net.phys2d.raw.World;
import net.phys2d.raw.shapes.Line;
import net.phys2d.raw.strategies.QuadSpaceStrategy;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.tiled.TiledMap;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import net.phys2d.raw.Body;
import net.phys2d.raw.CollisionListener;

/**
 *
 * @author User
 */
public class GameWorld{
    private ArrayList<Sprite> sprites = new ArrayList();
    private int               height;
    private TiledMap          map;
    private int               tileHeight;
    private int               tileWidth;
    private StaticBody        top, bottom, left, right;
    private int               width;
    private World             world;

    public GameWorld(TiledMap map) {
        this.map   = map;
        world      = new World(new Vector2f(0.0f, 0.0f), 10, new QuadSpaceStrategy(20, 5));
        tileWidth  = map.getTileWidth();
        tileHeight = map.getTileHeight();
        height     = map.getHeight() * tileHeight;
        width      = map.getWidth() * tileWidth;
        top        = new StaticBody("topBoundary", new Line(0, 0, width, 0));
        bottom     = new StaticBody("bottomBoundary", new Line(0, height, width, height));
        left       = new StaticBody("leftBoundary", new Line(0, 0, 0, height));
        right      = new StaticBody("rightBoundary", new Line(width, 0, width, height));
       
        world.add(top);
        world.add(bottom);
        world.add(left);
        world.add(right);
    }

    public void addSprite(Sprite sprite) {
        world.add(sprite);
        sprites.add(sprite);
    }

    public void addListener(CollisionListener cl){
        world.addListener(cl);
    }

    public void render(Graphics g, Sprite leadSprite) {
        g.translate(-leadSprite.getPosition().getX() +400, -leadSprite.getPosition().getY() + 300);
        map.render(0, 0);

        for (int i = 0; i < sprites.size(); i++) {
            Sprite sprite = sprites.get(i);

            sprite.draw();
        }

        RenderUtil.drawWorld(g, world);
    }

    public void update() {
       
        world.step();
    }

   
}


//~ Formatted by Jindent --- http://www.jindent.com
